package com.syerit.modules.utils
{
	import __AS3__.vec.Vector;
	
	import flash.display.DisplayObject;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.events.ModuleEvent;
	import mx.modules.IModuleInfo;
	import mx.modules.ModuleManager;
	
	public class ModulePageManager extends Canvas 
	{
		public var dataProvider:ArrayCollection;
		public var transitionManager:ITransitionManager;
		
		private var modules:Vector.<IModuleInfo>;
		private var moduleIndex:int=-1;
		
		public function ModulePageManager()
		{
			super();
		}
		public function activate():void
		{
			trace(dataProvider);
			for(var i:int=0;i<dataProvider.length;i++)
			{
				addChild(new Canvas());
			}
			modules = new Vector.<IModuleInfo>(dataProvider.length);
		}
		public function changeModule(i:int):void
		{
			//if selected module != current module
			
			if(i!=moduleIndex)
			{
				transitionManager.showLoader();

				//if current module exist
				if(moduleIndex!=-1)
				{
					removeCurrentModule(moduleIndex);
					//transitionManager.outgoingDisplayObject=IModuleComponent(Canvas(getChildAt(moduleIndex)).getChildAt(0));
				}
				//if new module notexist
				if(modules[i]==null)
				{
					loadNewModule(i);
				}
				//if new module exist
				else
				{
					IModuleComponent(Canvas(getChildAt(i)).getChildAt(0)).activate();
					transitionManager.outgoingDisplayObject=IModuleComponent(Canvas(getChildAt(i)).getChildAt(0));
					transitionManager.hideLoaderAndChange();
				}
				//display new module
				moduleIndex = i;
				//this.selectedIndex = i;
			}
		}
		//############# NEW MODULE ##################
		private function loadNewModule(i:int):void
        {
        	var module:IModuleInfo = ModuleManager.getModule(dataProvider[i]+"?rand="+(100*Math.random()));
            module.addEventListener(ModuleEvent.READY, onModuleReady);
            modules[i]=module;
            module.load();
        }
        private function onModuleReady(e:ModuleEvent):void
		{
			var module:IModuleComponent = modules[moduleIndex].factory.create() as IModuleComponent;
			Canvas(getChildAt(moduleIndex)).addChild(module as DisplayObject);
			module.modelProxy = null;
			//module.serviceProxy = serviceProxy;
			module.addEventListener(ModulePageEvent.CONTENT_LOADED,onContentLoaded);
			module.loadAppData();
		}
		private function onContentLoaded(e:ModulePageEvent):void
		{
			trace("com.modules.utils.ModulePageManager: ModulePage content ready");
			e.target.preactivate();
			transitionManager.ongoingDisplayObject= IModuleComponent(e.target);
			transitionManager.hideLoaderAndChange();
			e.target.activate();
			
			//transitionManager.hideLoader();
		}
        
        //############# CURRENT MODULE ##################
        private function removeCurrentModule(i:int):void
        {
        	if(IModuleComponent(Canvas(getChildAt(i)).getChildAt(0)).reloadable==true)
        	{
        		unloadCurrentModule(i);
        	}
        	else
        	{
        		hideCurrentModule(i);
        	}
        } 
        private function unloadCurrentModule(i:int):void
        {
        	IModuleComponent(Canvas(getChildAt(i)).getChildAt(0)).destroy();
        	Canvas(getChildAt(i)).removeChildAt(0);
        	modules[i].unload();
        	modules[i].removeEventListener(ModuleEvent.READY, onModuleReady);
        	modules[i]=null;
        }
        private function hideCurrentModule(i:int):void
        {
        	IModuleComponent(Canvas(getChildAt(i)).getChildAt(0)).deactivate();
        }
        //############# OTHER ##################
		private function onTransitionsFinished():void
		{
			
		}
		private function onModuleUnloaded(e:ModuleEvent):void
		{
			//moduleLoader.removeEventListener(ModuleEvent.READY,onModuleReady);
			//moduleLoader.removeEventListener(ModuleEvent.UNLOAD,onModuleUnloaded);
		}
	}
}